#include <google/protobuf/service.h>
#include <random>
#include "fastrpc/rpc.hpp"
#include "order.pb.h"
using namespace fastrpc::async;
using namespace fastrpc::net;
using namespace fastrpc::log;
using namespace fastrpc;
using namespace fastrpc::rpc;

class OrderImpl : public Order {
public:
    void makeOrder(google::protobuf::RpcController* controller,
                   const ::makeOrderRequest*        request,
                   ::makeOrderResponse*             response,
                   ::google::protobuf::Closure*     done) {
        if (request->price() < 0) {
            log::console.info("makeOrder failed.");
            response->set_ret_code(-1);
            response->set_order_id("no");
            return;
        }
        std::random_device              rd;
        std::mt19937                    gen(rd());
        std::uniform_int_distribution<> dis(100000000, 999999999);
        int                             random_number = dis(gen);
        std::stringstream               ss;
        ss << random_number;
        std::string random_string = ss.str();
        response->set_order_id(random_string);
        log::console.info("makeOrder success.");
    }
};

auto main() -> int {
    SET_LOG_LEVEL(fastrpc::log::LogLevel::Debug);
    RpcServer                  server{10000};
    std::shared_ptr<OrderImpl> service = std::make_shared<OrderImpl>();
    server.register_service(service);
    server.run();
}